home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / p4 / p4-1_2c.lha / p4-1.2c / lib / p4_sock_cr.c < prev    next >
C/C++ Source or Header  |  1993-05-25  |  7KB  |  262 lines

  1. #include "p4.h"
  2. #include "p4_sys.h"
  3.  
  4. int create_remote_processes(pg)
  5. struct p4_procgroup *pg;
  6. {
  7.     struct p4_procgroup_entry *pe;
  8.     int i, rm_num = 1;
  9.  
  10.     for (i = 0, pe = pg->entries; i < pg->num_entries; i++, pe++)
  11.     {
  12.     if (strcmp(pe->host_name, "local") != 0)
  13.     {
  14.         create_one_remote(pe, rm_outfile_head, rm_num++);
  15.     }
  16.     }
  17.  
  18.     return (0);
  19. }
  20.  
  21. P4VOID create_one_remote(pe, outfile, rm_num)
  22. struct p4_procgroup_entry *pe;
  23. char *outfile;
  24. int rm_num;
  25. {
  26.     int rm_fd;
  27.     struct bm_rm_msg msg;
  28.     P4BOOL done;
  29.     int type;
  30.     int status, port, remote_switch_port;
  31.     int slave_idx, slave_pid, pidx, rm_ind;
  32.  
  33.     rm_fd = net_create_slave(pe->host_name,
  34.                  pe->slave_full_pathname,
  35.                  pe->username);
  36.  
  37.     msg.type = p4_i_to_n(INITIAL_INFO);
  38.     msg.numinproctab = p4_i_to_n(p4_global->num_in_proctable);
  39.     msg.numslaves = p4_i_to_n(pe->numslaves_in_group);
  40.     strcpy(msg.outfile, outfile);
  41.     msg.rm_num = p4_i_to_n(rm_num);
  42.     msg.debug_level = p4_i_to_n(remote_debug_level);
  43.     msg.memsize = p4_i_to_n(globmemsize);
  44.     msg.logging_flag = p4_i_to_n(logging_flag);
  45.     strcpy(msg.application_id, p4_global->application_id);
  46.     strcpy(msg.version, p4_version());
  47.     strcpy(msg.pgm, pe->slave_full_pathname);
  48.  
  49.     net_send(rm_fd, &msg, sizeof(msg), FALSE);
  50.  
  51.     port = -1;
  52.     pidx = -1;
  53.     for (done = FALSE; !done;)
  54.     {
  55.     status = net_recv(rm_fd, &msg, sizeof(msg));
  56.     if (status == PRECV_EOF)
  57.     {
  58.         p4_dprintf("OOPS! got EOF in create_one_remote\n");
  59.         return;
  60.     }
  61.  
  62.  
  63.     type = p4_n_to_i(msg.type);
  64.     switch (type)
  65.     {
  66.       case REMOTE_LISTENER_INFO:
  67.         port = p4_n_to_i(msg.port);
  68.         break;
  69.  
  70.       case REMOTE_MASTER_INFO:
  71.       case REMOTE_SLAVE_INFO:
  72.         if (type == REMOTE_MASTER_INFO)
  73.            rm_ind = TRUE;
  74.         else
  75.            rm_ind = FALSE;
  76.         slave_idx = p4_n_to_i(msg.slave_idx);
  77.         slave_pid = p4_n_to_i(msg.slave_pid);
  78.         remote_switch_port = p4_n_to_i(msg.switch_port);
  79.         if (port == -1)
  80.         p4_dprintf("OOPS! got slave_info w/o getting port first\n");
  81.         /* big master installing remote processes */
  82.         pidx = install_in_proctable(rm_num,port,slave_pid,
  83.                     msg.host_name,slave_idx,
  84.                     msg.machine_type,remote_switch_port);
  85.             p4_dprintfl(90, "create_one_remote: adding connection: %d --> %d\n",
  86.                 p4_get_my_id(), pidx);
  87.  
  88.             if (p4_local->conntab[pidx].type == CONN_REMOTE_SWITCH)
  89.             {
  90.             p4_local->conntab[pidx].switch_port = remote_switch_port;
  91.             p4_local->conntab[pidx].port = rm_fd;
  92.             }
  93.             else if (p4_local->conntab[pidx].type == CONN_REMOTE_NON_EST)
  94.             {
  95.         if (type == REMOTE_MASTER_INFO)
  96.         {
  97.                 p4_local->conntab[pidx].type = CONN_REMOTE_EST;
  98.                 p4_local->conntab[pidx].port = rm_fd;
  99.             p4_local->conntab[pidx].same_data_rep =
  100.             same_data_representation(p4_local->my_id,pidx);
  101.         }
  102.             }
  103.             else
  104.             {
  105.             p4_error("create_one_remote: invalid conn type in conntab\n",
  106.                  p4_local->conntab[pidx].type);
  107.             }
  108.         break;
  109.  
  110.       case REMOTE_SLAVE_INFO_END:
  111.         done = TRUE;
  112.         break;
  113.     }
  114.     }
  115.  
  116. }
  117.  
  118.  
  119. /*
  120.  *    Run the slave pgm on host; returns the file descriptor of the
  121.  *    connection to the slave.
  122.  */
  123. int net_create_slave(host, pgm, username)
  124. char *host;
  125. char *pgm;
  126. char *username;
  127. {
  128.     int serv_port, serv_fd;
  129.     int slave_fd;
  130.     int slave_pid, master_pid;
  131.     char hostname[100];
  132.     struct net_initial_handshake hs;
  133.     char myhost[100];
  134.     int connection_fd, success, rc;
  135.     struct net_message_t msg;
  136.     char remote_shell[64];
  137.     char serv_port_c[64];
  138.     char *am_slave_c = "-p4amslave";
  139. #if defined(SYMMETRY) || defined(SUN) || \
  140.     defined(DEC5000)  || defined(SGI) || \
  141.     defined(RS6000)   || defined(HP)  || \
  142.     defined(NEXT)     || defined(CRAY) || \
  143.     defined(CONVEX)   || defined(KSR)  || \
  144.     defined(FX2800)   || defined(FX2800_SWITCH)  || \
  145.     defined(SP1)
  146.     char *getpw();
  147. #endif
  148.  
  149.     net_setup_anon_listener(5, &serv_port, &serv_fd);
  150.  
  151.     myhost[0] = '\0';
  152.     get_qualified_hostname(myhost);
  153.  
  154.     if (no_remotes)
  155.     {
  156.     printf("waiting for process on host %s:\n%s %s %d %s\n",
  157.            host, pgm, myhost, serv_port, am_slave_c);
  158.         rc = 0;
  159.     }
  160.     else
  161.     {
  162.     /* try to connect to (secure) server */
  163.  
  164. #if defined(SYMMETRY) || defined(SUN) || \
  165.     defined(DEC5000)  || defined(SGI) || \
  166.     defined(RS6000)   || defined(HP)  || \
  167.     defined(NEXT)     || defined(CRAY) || \
  168.     defined(CONVEX)   || defined(KSR)  || \
  169.     defined(FX2800)   || defined(FX2800_SWITCH)  || \
  170.     defined(SP1)
  171.  
  172.     /*****  secure server stuff  *******/
  173.     p4_dprintfl(20, "trying to create remote slave on %s via server\n",host);
  174.     rc = start_slave(host, username, pgm, serv_port, am_slave_c, getpw);
  175.  
  176.     if (rc < -1)
  177.     {
  178.         extern char *start_prog_error;
  179.  
  180.         p4_dprintf("Warning from secure server: %s\n", start_prog_error);
  181.     }
  182.     else if (rc == 0)
  183.         p4_dprintfl(10, "created remote slave on %s via server\n",host);
  184.     /*****************************************/
  185. #else
  186.     rc = -1;
  187. #endif
  188.     }
  189.  
  190.     if (rc <= -1)
  191.     {
  192.     /* try to connect to (old) server */
  193.     connection_fd = net_conn_to_listener(host, UNRESERVED_PORT, 1);
  194.  
  195.     if (connection_fd >= 0)
  196.     {
  197.         p4_dprintfl(20, "creating remote slave on %s via old server\n",host);
  198.         msg.type = p4_i_to_n(NET_EXEC);
  199.         strcpy(msg.pgm, pgm);
  200.         strcpy(msg.host, myhost);
  201.         strcpy(msg.am_slave, am_slave_c);
  202.         msg.port = p4_i_to_n(serv_port);
  203.         net_send(connection_fd, &msg, sizeof(msg), FALSE);
  204.         net_recv(connection_fd, &msg, sizeof(msg));
  205.  
  206.         success = p4_n_to_i(msg.success);
  207.         if (!success)
  208.         {
  209.         p4_dprintf("create failed: %s\n", msg.message);
  210.         return (-1);
  211.         }
  212.         close(connection_fd);
  213.         p4_dprintfl(10, "created remote slave on %s via old server\n",host);
  214.     }
  215.     else
  216.     {
  217. #if defined(DELTA)
  218.         p4_dprintf("delta cannot create remote processes\n");
  219. #else
  220.         p4_dprintfl(20, "creating remote slave on %s via remote shell\n",host);
  221. #ifdef P4BSD
  222.         strcpy(remote_shell, "rsh");
  223. #endif
  224.  
  225. #ifdef P4SYSV
  226. #    if defined(TITAN) || defined(SGI)
  227.         strcpy(remote_shell, "rsh");
  228. #    else
  229. #        if defined(SYMMETRY_PTX)
  230.         strcpy(remote_shell, "resh");
  231. #        else
  232.         strcpy(remote_shell, "remsh");
  233. #        endif
  234. #    endif
  235. #endif
  236.  
  237.         sprintf(serv_port_c, "%d", serv_port);
  238.         rc = fork_p4();
  239.         if (rc == 0)
  240.         {
  241.         rc = execlp(remote_shell, remote_shell,
  242.                 host, "-l", username, "-n", pgm,
  243.                 myhost, serv_port_c, am_slave_c, 0);
  244.         /* host,"-n","cluster","5",pgm,myhost,serv_port_c,0); for butterfly */
  245.         if (rc < 0)
  246.             p4_error("net_create_slave: execlp", rc);
  247.         }
  248.         p4_dprintfl(10, "created remote slave on %s via remote shell\n",host);
  249. #endif
  250.     }
  251.     }
  252.     slave_fd = net_accept(serv_fd);
  253.     close(serv_fd);
  254.  
  255.     hs.pid = (int) htonl(getpid());
  256.     net_send(slave_fd, &hs, sizeof(hs), FALSE);
  257.     net_recv(slave_fd, &hs, sizeof(hs));
  258.     slave_pid = (int) ntohl(hs.pid);
  259.  
  260.     return slave_fd;
  261. }
  262.